//
//  ChooseTimeView.m
//  Common
//
//  Created by shiqing guo on 2018/8/13.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "ChooseTimeView.h"
#import "RightDatePicker.h"
#import "FSCalendar.h"
#import "UILabel+Extension.h"
#import "Macro.h"

#define TIMESEPARATED @" - "

@interface ChooseTimeView ()<UIScrollViewDelegate>

@property (nonatomic ,assign) NSInteger selectIndex;

@property (nonatomic ,strong) UIScrollView *timeScrollView;

@property (nonatomic ,strong) RightDatePicker *rightDatePicker;


/** 整体 View */
@property (nonatomic ,strong) UIView *bgView;

/** 上方显示时间区域 */
@property (nonatomic ,strong) UIView *upView;

/** 时间 : 00:00 */
@property (nonatomic ,strong) UILabel *timerLabel;

/** 线 */
@property (nonatomic ,strong) UILabel *shortLineLabel;
/** 确定按钮 */
@property (nonatomic, strong) UIButton *sureBtn;

@end

@implementation ChooseTimeView
#pragma mark - UI
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]; ;
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissAction)]];
        
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.upView];
        
        [self.upView addSubview:self.timerLabel];
        [self.upView addSubview:self.shortLineLabel];
        [self.upView addSubview:self.sureBtn];
        
        [self.bgView addSubview:self.timeScrollView];
        [self customScrollerView];
    }
    return self;
}

#pragma mark - 懒加载
- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        [_bgView setFrame:CGRectMake(0, self.frame.size.height - 408, UI_SCREEN_WIDTH, 408)];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UIView *)upView
{
    if (!_upView) {
        _upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 48)];
        _upView.backgroundColor = RGB_HEX(0xf6f6f6);
    }
    return _upView;
}

- (UILabel *)timerLabel
{
    if (!_timerLabel) {
        _timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 120, 48)];
        _timerLabel.textColor = RGB_HEX(0x606060);
        _timerLabel.font = SystemFont(16.0f);
        _timerLabel.userInteractionEnabled = YES;
        _timerLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _timerLabel;
}

- (UILabel *)shortLineLabel
{
    if (!_shortLineLabel) {
        _shortLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.upView.frame)-1.5, [self.timerLabel textWidth], 1.5)];
        _shortLineLabel.backgroundColor = RGB_HEX(0x838381);
    }
    return _shortLineLabel;
}

- (UIButton *)sureBtn
{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:(UIControlStateNormal)];
        [_sureBtn setTitleColor:RGB_HEX(0x4d92d5) forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = SystemFont(16.0f);
        _sureBtn.frame = CGRectMake(UI_SCREEN_WIDTH - 56/2 - 35, 0, 35, 48);
        [_sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _sureBtn;
}

- (UIScrollView *)timeScrollView
{
    if (!_timeScrollView) {
        _timeScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.upView.frame) + 10, UI_SCREEN_WIDTH, 720/2)];
        _timeScrollView.contentSize = CGSizeMake(UI_SCREEN_WIDTH *2, 720/2);
        _timeScrollView.pagingEnabled = YES;
        _timeScrollView.scrollEnabled = NO;
        _timeScrollView.bounces = NO;
    }
    return _timeScrollView;
}

- (void)customScrollerView
{
    RightDatePicker *rightDatePicker = [[RightDatePicker alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 720/2)];
    WEAKSELF
    rightDatePicker.timerBlock = ^(NSString *strStart, NSString *strEnd) {
        weakSelf.timerLabel.text = [NSString stringWithFormat:@"%@%@%@", strStart, TIMESEPARATED, strEnd];
        weakSelf.shortLineLabel.frame = CGRectMake(15, CGRectGetMaxY(weakSelf.upView.frame)-1.5, [weakSelf.timerLabel textWidth], 1.5);
    };
    [self.timeScrollView addSubview:rightDatePicker];
    self.rightDatePicker = rightDatePicker;
}


#pragma mark - 展示与隐藏
/** 展示从底部向上弹出的UIView（包含遮罩） */
- (void)showInView:(UIView *)view
{
    if (!view) return;
    
    [view addSubview:self];
    [view addSubview:self.bgView];
    [self.bgView setFrame:CGRectMake(0, view.frame.size.height, UI_SCREEN_WIDTH, (408))];
    
    WEAKSELF
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.alpha = 1.0;
        [weakSelf.bgView setFrame:CGRectMake(0, view.frame.size.height - (408), UI_SCREEN_WIDTH, (408))];
    } completion:nil];
}

- (void)disMissAction
{
    if (self.dismissBlock) {
        self.dismissBlock();
    }
    [self disMissView];
}


/** 移除从上向底部弹下去的UIView（包含遮罩） */
- (void)disMissView
{
    [self.bgView setFrame:CGRectMake(0, self.frame.size.height - (408), UI_SCREEN_WIDTH, (408))];
    
    WEAKSELF
    [UIView animateWithDuration:0.3f
                     animations:^{
                         weakSelf.alpha = 0.0;
                         [weakSelf.bgView setFrame:CGRectMake(0, weakSelf.frame.size.height, UI_SCREEN_WIDTH, (408))];
                     }
                     completion:^(BOOL finished){
                         [weakSelf removeFromSuperview];
                         [weakSelf.bgView removeFromSuperview];
                     }];
}




#pragma mark - BtnCilck
- (void)sureBtnClick:(UIButton *)sender
{
    NSArray *timeList = [self.timerLabel.text componentsSeparatedByString:TIMESEPARATED];
    NSString *strBeginTime = [timeList objectAtIndex:0];
    NSString *strEndTime = [timeList objectAtIndex:1];
    [self disMissAction];
    if (self.chooseTimeWithTimerBlock) {
        self.chooseTimeWithTimerBlock(strBeginTime, strEndTime);
    }
}

#pragma mark - Set
/** 时间 */
- (void)setNowTimerStr:(NSString *)nowTimerStr
{
    _nowTimerStr = nowTimerStr;
    self.timerLabel.text = [NSString stringWithFormat:@"%@%@%@", nowTimerStr, TIMESEPARATED, nowTimerStr];
    self.rightDatePicker.timerStr = _nowTimerStr;
    self.shortLineLabel.frame = CGRectMake(15, CGRectGetMaxY(self.upView.frame)-1.5, [self.timerLabel textWidth], 1.5);
}

@end
